// 老师点评作业
import express from "express";
import pool from "../../importantApi/db_connection.js";

const router = express.Router();
router.post("/", async (req, res) => {
    let {doworkId, score, comment, createtime} = req.body;
    if (!doworkId) {
        return res.status(400).json({
            code: 401,
            msg: "缺少必要参数"
        })
    }
    score = score || ''
    comment = comment || ''
    if (!createtime) {
        let date = new Date()
        const year = date.getFullYear()
        const month = date.getMonth() + 1
        const day = date.getDate()
        const hour = date.getHours()
        const minute = date.getMinutes()
        createtime = `${year}-${String(month).padStart(2,'0')}-${String(day).padStart(2,'0')} ${String(hour).padStart(2,'0')}:${String(minute).padStart(2,'0')}`
    }
    try {
        await pool.query(`
            insert into reviewwork (dowork_id, score, comment, createtime)
            values (${doworkId}, '${score}', '${comment}', '${createtime}')
        `)
        return res.status(200).json({
            code: 200,
            msg: "点评提交完成"
        })
    } catch (err) {
        console.log(err)
        return res.status(500).json({
            code: 500,
            msg: "提交错误：server error"
        })
    }

})
export default router;